package com.itheima.mp.controller;

import cn.hutool.core.bean.BeanUtil;
import com.itheima.mp.domain.dto.UserFormDTO;
import com.itheima.mp.domain.dto.UserQuery;
import com.itheima.mp.domain.po.User;
import com.itheima.mp.domain.vo.UserVO;
import com.itheima.mp.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: Bytezero_zhengLei
 * @Date: 2024/7/25 12:33
 * @Project_Name:
 * @Email: 420498246@qq.com
 * @Description:
 * @TODO:
 */

@RestController
@RequestMapping("/users")
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping
    public void createUser(@RequestBody UserFormDTO userFormDTO) {
        User user = BeanUtil.copyProperties(userFormDTO, User.class);
        userService.save(user);
    }

    @DeleteMapping("/{id}")
    public void deleteUserById(@PathVariable("id") Long id) {
        userService.removeById(id);
    }

//    @GetMapping("/{id}")
//    public UserVO getUserById(@PathVariable Long id) {
//        //获取po数据
//        User user = userService.getById(id);
//        //把po转成 vo
//        return BeanUtil.copyProperties(user, UserVO.class);
//    }
    //静态DB  查询用户及其地址
    @GetMapping("/{id}")
    public UserVO getUserById2(@PathVariable Long id) {
        //获取po数据
        UserVO user = userService.getUserAndAddress(id);

        //把po转成 vo
        return user;
    }




    @GetMapping
    public List<UserVO> getUsers(@RequestParam List<Integer> ids) {

        //返回PO
        List<User> users = userService.listByIds(ids);

        //po 装 vo
        return BeanUtil.copyToList(users, UserVO.class);

    }

    @PutMapping("/{id}/deduction/{money}")
    public void updateMoney(@PathVariable Long id, @PathVariable Integer money) {

        //调用自己的 updateMoney
        userService.updateMoney(id, money);

    }


    @GetMapping("/list")
    public List<UserVO> listUsers(@RequestBody UserQuery userQuery) {

        //业务层条件查询
        List<User> users = userService.selectUsers(userQuery.getName(), userQuery.getStatus(), userQuery.getMinBalance(), userQuery.getMaxBalance());

        //把po 转 Vo
        List<UserVO> userVOS = BeanUtil.copyToList(users, UserVO.class);

        return  userVOS;
    }



}
